package com.yy.jhl.ui.manager.adapter;

import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.yy.common.base.BaseAdapter;
import com.yy.common.commonwidget.OnNoDoubleClickListener;
import com.yy.jhl.R;
import com.yy.jhl.bean.CGSOrderInfo;
import com.yy.jhl.bean.CGSOrderInfo;
import com.yy.jhl.interfaces.CommonCallBack;
import com.yy.jhl.interfaces.DoneOrderCallBack;
import com.yy.jhl.ui.manager.activity.JGOrderActivity;
import com.yy.jhl.util.DataRequestUtil;
import com.yy.jhl.util.DialogUtil;

import java.util.ArrayList;
import java.util.List;


/**
 * Created by Ellis.Li on 2017/6/15.
 */

public class JGOrderAdapter extends BaseAdapter {
    private List<CGSOrderInfo> modelList;
    private boolean isDone = false;
    DoneOrderCallBack commonCallBack;
    public JGOrderAdapter(Context context, boolean isDone,DoneOrderCallBack commonCallBack) {
        super(context);
        this.isDone = isDone;
        this.commonCallBack = commonCallBack;
        modelList = new ArrayList<>();
    }
    @Override
    public void removeItem(int position) {
        super.removeItem(position);
        modelList.remove(position);
        notifyDataSetChanged();
    }
    @Override
    public void clearData() {
        super.clearData();
        modelList.clear();
        notifyDataSetChanged();
    }
    public void addData(List<CGSOrderInfo> monomerModels) {
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }
    public void addData(CGSOrderInfo info) {
        modelList.add(0,info);
        notifyDataSetChanged();
    }

    public void addAllData(List<CGSOrderInfo> monomerModels) {
        clearData();
        modelList.addAll(monomerModels);
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return modelList.size();
    }

    @Override
    public Object getItem(int i) {
        return modelList.get(i);
    }

    @Override
    public long getItemId(int i) {
        return i;
    }

    @Override
    public View getView(final int i, View view, ViewGroup viewGroup) {
        Holder storeHolder;
        if(view == null){
            storeHolder = new Holder();
            view = layoutInflater.inflate(R.layout.item_jg_order,null);
            storeHolder.initView(view);
            view.setTag(storeHolder);
        }else{
            storeHolder = (Holder)view.getTag();
        }
        if (isDone){
            storeHolder.manager_text.setVisibility(View.GONE);
            storeHolder.status_red_img.setVisibility(View.GONE);
        }else {
            storeHolder.manager_text.setVisibility(View.GONE);
            storeHolder.status_red_img.setVisibility(View.GONE);
        }
        final CGSOrderInfo itemModel = modelList.get(i);
        storeHolder.order_name_text.setText(itemModel.getProject_name());
        storeHolder.order_code_text.setText(itemModel.getProject_num());
        storeHolder.order_doing_text.setText(itemModel.getBegin_order());
        storeHolder.order_done_text.setText(itemModel.getEnd_order());
        storeHolder.look_order_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //查看详情
                JGOrderActivity.startAction(mContext,itemModel.getBegin_order(),itemModel.getEnd_order(),itemModel.getProject_id());
            }
        });
        storeHolder.manager_text.setOnClickListener(new OnNoDoubleClickListener() {
            @Override
            protected void onNoDoubleClick(View v) {
                //完结
                DialogUtil.showEnsureDialogView(mContext, "是否确定完结该项目？", new CommonCallBack() {
                    @Override
                    public void getCallBack() {
                        DataRequestUtil.getInstance(mContext).cgsDoneOrder(itemModel.getProject_id(),"2",i,commonCallBack);
                    }
                });
            }
        });
        return view;
    }
    /**
     *
     */
    class Holder{
        private ImageView status_red_img;
        private TextView order_name_text;
        private TextView order_code_text;
        private TextView order_doing_text;
        private TextView order_done_text;
        private TextView look_order_text;
        private TextView manager_text;
        void initView(View view) {
            status_red_img = (ImageView) view.findViewById(R.id.status_red_img);
            order_name_text = (TextView) view.findViewById(R.id.order_name_text);
            order_code_text = (TextView) view.findViewById(R.id.order_code_text);
            order_doing_text = (TextView) view.findViewById(R.id.order_doing_text);
            order_done_text = (TextView) view.findViewById(R.id.order_done_text);
            look_order_text = (TextView) view.findViewById(R.id.look_order_text);
            manager_text = (TextView) view.findViewById(R.id.manager_text);
        }

    }
}
